//
//  HJBaseTabBar.swift
//  HJSinaWeibo
//
//  Created by 夏航军 on 17/9/3.
//  Copyright © 2017年 夏航军. All rights reserved.
//

import UIKit

class HJBaseTabBar: UITabBar {
    
    //闭包回调
    var composeClosure : (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        addSubview(composeBtn);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let btnW = self.bounds.width / 5;
        let btnH = self.bounds.height;
        var index = 0;
        
        for subview in subviews {
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRect(x: btnW * CGFloat(index)  , y: 0, width: btnW, height: btnH);
                
                index += (index == 1) ? 2 : 1;
            }
        }
        
        composeBtn.bounds = CGRect(x: 0, y: 0, width: btnW, height: btnH);
        composeBtn.center = CGPoint(x: self.center.x, y: btnH * 0.5);
    }
    
    ///点击自定义按钮
    func didClickComposeBtn() {
        composeClosure?()
    }

    
    
    lazy var composeBtn : UIButton = {
        let btn = UIButton();
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal);
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted);
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal);
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted);
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(didClickComposeBtn), for: .touchUpInside);
        return btn;
    }()

}
